package day_2022_7_to_8.day48;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author haomin
 * @date 2022/08/31 20:42
 **/
public class Day46_11_Offer32_LevelOrder {
    class Solution {
        public int[] levelOrder(TreeNode root) {
            if(root == null) return new int[0];
            List<Integer> list = new ArrayList<>();
            Queue<TreeNode> queue = new LinkedList<>();
            queue.offer(root);
            while(!queue.isEmpty()){
                int size = queue.size();
                for(int i = 0; i < size; ++i){
                    TreeNode cur = queue.poll();
                    list.add(cur.val);
                    if(cur.left != null) queue.offer(cur.left);
                    if(cur.right != null) queue.offer(cur.right);
                }
            }
            int len = list.size();
            int[] ans = new int[len];
            for(int i = 0;i < len; ++i){
                ans[i] = list.get(i);
            }
            return ans;
        }
    }
}